package com.thunder.reader.ui;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.thunder.reader.FeedSettings;
import com.thunder.reader.R;
import com.thunder.reader.provider.Feed;

public class ViewOriginalArticleActivity extends Activity {
    protected static final String TAG = FeedSettings.GLOBAL_TAG;
    
    private Uri mArticleUri;
    private String mArticleUrl;
    private WebView mWebview;
    private boolean mLoadingUrl;
    
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        final Intent intent = getIntent();
        if (intent == null) {
            finish();
        }
        mArticleUri = intent.getData();
        if (mArticleUri == null) {
            finish();
        }
        queryArticleUrl();
        setContentView(R.layout.view_original_article);
        mWebview = (WebView) findViewById(R.id.view_article_webview);
        final ProgressBar progressBar = (ProgressBar) findViewById(R.id.view_article_progressbar);
        mWebview.setWebChromeClient(new WebChromeClient() {
           public void onProgressChanged(WebView view, int progress) {
               progressBar.setProgress(progress);
               if (progress == 100) {
        	   progressBar.setVisibility(View.GONE);
                   mLoadingUrl = false;
               } else {
                   mLoadingUrl = true;
               }
           }
        });
        mWebview.setWebViewClient(new WebViewClient() { 
           public void onReceivedError(WebView view, int errorCode, String description, String failureUrl) {
//               Toast.makeText(ViewArticleActivity.this, description + " on url " + failureUrl, Toast.LENGTH_LONG).show();
               Log.e(TAG, description + " on url " + failureUrl);
           }
        });
        mWebview.getSettings().setJavaScriptEnabled(true);
        mWebview.loadUrl(mArticleUrl);
        mLoadingUrl = true;
    }

    private void queryArticleUrl() {
        Cursor cursor = null;
        try {
            cursor = getContentResolver().query(mArticleUri, new String[]{Feed.Article.ARTICLE_URL}, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                mArticleUrl = cursor.getString(0);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        if (TextUtils.isEmpty(mArticleUrl)) {
            Toast.makeText(getApplication(), R.string.invalid_url, Toast.LENGTH_SHORT).show();
            finish();
        }
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.view_original_article, menu);
        return true;
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if (TextUtils.isEmpty(mArticleUrl)) {
            menu.findItem(R.id.view_original_view_in_browser).setEnabled(false);
        }
        if (mLoadingUrl) {
            menu.findItem(R.id.view_original_stop).setTitle(R.string.stop);
        } else {
            menu.findItem(R.id.view_original_stop).setTitle(R.string.refresh);
        }
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.view_original_stop: {
            if (mLoadingUrl) {
                mWebview.stopLoading();
            } else {
                mWebview.reload();
            }
            break;
        }
        case R.id.view_original_view_in_browser: {
            final Uri uri = Uri.parse(mArticleUrl);
            final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
            break;
        }
        default:
            break;
        }
        return true;
    }

    @Override
    protected void onDestroy() {
	if (mWebview != null) {
	    if (mLoadingUrl) {
		mWebview.stopLoading();
	    }
	    mWebview.destroy();
	}
	super.onDestroy();
    }

    @Override
    public void onBackPressed() {
	super.onBackPressed();
	overridePendingTransition(R.anim.default_leave_enter, R.anim.default_leave_exit);
    }
}
